package s2elinshu.swing;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;

public class JoinMember extends JDialog {


	private final JPanel contentPanel = new JPanel();
	private JTextField uid;
	private JPasswordField pwd;
	private JPasswordField repwd;
	private JTextField zipcode;
	private JTextField addr;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			JoinMember dialog = new JoinMember();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public JoinMember() {
		setIconImage(Toolkit.getDefaultToolkit().getImage(JoinMember.class.getResource("/images/icon/icon.png")));
		setTitle("회원가입");
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		
		JLabel label = new JLabel("");
		
		JLabel lblUid = new JLabel("아이디");
		
		uid = new JTextField();
		uid.setColumns(10);
		
		JButton dpuBtn = new JButton("중복확인");
		
		JLabel lblPwd = new JLabel("비밀번호");
		
		pwd = new JPasswordField();
		
		JLabel lblRepwd = new JLabel("비밀번호 확인");
		
		repwd = new JPasswordField();
		
		JLabel lblZipcode = new JLabel("우편번호");
		
		zipcode = new JTextField();
		zipcode.setEditable(false);
		zipcode.setColumns(10);
		
		JButton zipBtn = new JButton("우편번호 검색");
		zipBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JoinMember parent = (JoinMember) SwingUtilities.windowForComponent( (Component) e.getSource() );
				FindZipcode modal = new FindZipcode(parent);
				modal.setVisible(true);
			}
		});
		
		addr = new JTextField();
		addr.setColumns(10);
		
		JLabel lblAddr = new JLabel("기본주소");
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_contentPanel.createSequentialGroup()
					.addGap(34)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(lblAddr)
							.addPreferredGap(ComponentPlacement.RELATED))
						.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
							.addGroup(gl_contentPanel.createSequentialGroup()
								.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
									.addComponent(lblPwd)
									.addComponent(lblRepwd)
									.addGroup(gl_contentPanel.createSequentialGroup()
										.addComponent(lblUid, GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE)
										.addGap(40)))
								.addPreferredGap(ComponentPlacement.RELATED)
								.addComponent(label)
								.addPreferredGap(ComponentPlacement.RELATED))
							.addGroup(gl_contentPanel.createSequentialGroup()
								.addComponent(lblZipcode)
								.addGap(43))))
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPanel.createParallelGroup(Alignment.TRAILING, false)
							.addComponent(pwd, Alignment.LEADING)
							.addComponent(repwd, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 116, Short.MAX_VALUE))
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
								.addComponent(uid, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(zipcode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
							.addGap(28)
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
								.addComponent(zipBtn)
								.addComponent(dpuBtn)))
						.addComponent(addr, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(46))
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(23)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(label)
							.addGap(15))
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
								.addGroup(gl_contentPanel.createSequentialGroup()
									.addComponent(lblUid)
									.addGap(18)
									.addComponent(lblPwd))
								.addGroup(gl_contentPanel.createSequentialGroup()
									.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
										.addComponent(uid, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
										.addComponent(dpuBtn))
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addComponent(pwd, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
							.addGap(12)
							.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblRepwd)
								.addComponent(repwd, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))
					.addGap(18)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblZipcode)
						.addComponent(zipcode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(zipBtn))
					.addPreferredGap(ComponentPlacement.RELATED, 21, Short.MAX_VALUE)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblAddr)
						.addComponent(addr, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(31))
		);
		contentPanel.setLayout(gl_contentPanel);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.CENTER));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("   확인   ");
				okButton.setToolTipText("");
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("   취소   ");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}	// 생성자
	
	// FindZipcode에서 작성된 주소 정보를 JoinMember의 zipcode, addr로 보내기 위해 작성한 메서드 FindZipcode가 호출함!! (중요)
	public void sendZipcode(String zip) {
		zipcode.setText( zip.split(" ") [0] );			// 빈칸으로 나눠서 맨 앞부분을 가져온다
		addr.setText( zip.substring(8) );
	}
}	// 클래스
